﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class SysLoginLogDA : ISysLoginLogDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string LOGIN_IP_ADDRESS = "@LOGIN_IP_ADDRESS";
        private string LOGIN_MAC_ADDRESS = "@LOGIN_MAC_ADDRESS";
        private string LOGIN_MACHINE_NAME = "@LOGIN_MACHINE_NAME";
        private string LOGIN_MODE = "@LOGIN_MODE";
        private string LOGIN_SEQUENCE = "@LOGIN_SEQUENCE";
        private string LOGIN_STATUS = "@LOGIN_STATUS";
        private string LOGIN_TIME = "@LOGIN_TIME";
        private string LOGIN_USER_ID = "@LOGIN_USER_ID";
        private string LOGIN_USER_NAME = "@LOGIN_USER_NAME";
        private string LOGOUT_TIME = "@LOGOUT_TIME";
        private string SQL_CHECK_SYS_LOGIN_LOG_ID_UNIQUE = " SELECT COUNT(1) FROM @_@SYS_LOGIN_LOG WHERE LOGIN_SEQUENCE = @LOGIN_SEQUENCE ";
        private string SQL_DELETE_SYS_LOGIN_LOG = " DELETE FROM @_@SYS_LOGIN_LOG WHERE LOGIN_SEQUENCE = @LOGIN_SEQUENCE ";
        private string SQL_INSERT_SYS_LOGIN_LOG = " INSERT INTO @_@SYS_LOGIN_LOG ( LOGIN_USER_ID, LOGIN_USER_NAME, LOGIN_STATUS, LOGIN_MODE, LOGIN_IP_ADDRESS, LOGIN_MACHINE_NAME, LOGIN_MAC_ADDRESS, LOGIN_TIME, LOGOUT_TIME, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @LOGIN_USER_ID, @LOGIN_USER_NAME, @LOGIN_STATUS, @LOGIN_MODE, @LOGIN_IP_ADDRESS, @LOGIN_MACHINE_NAME, @LOGIN_MAC_ADDRESS, @LOGIN_TIME, @LOGOUT_TIME, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_SYS_LOGIN_LOG = " SELECT  LOGIN_SEQUENCE, LOGIN_USER_ID, LOGIN_USER_NAME, LOGIN_STATUS, LOGIN_MODE, LOGIN_IP_ADDRESS, LOGIN_MACHINE_NAME, LOGIN_MAC_ADDRESS, LOGIN_TIME, LOGOUT_TIME, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@SYS_LOGIN_LOG ";
        private string SQL_UPDATE_SYS_LOGIN_LOG = " UPDATE @_@SYS_LOGIN_LOG SET LOGIN_USER_ID = @LOGIN_USER_ID, LOGIN_USER_NAME = @LOGIN_USER_NAME, LOGIN_STATUS = @LOGIN_STATUS, LOGIN_MODE = @LOGIN_MODE, LOGIN_IP_ADDRESS = @LOGIN_IP_ADDRESS, LOGIN_MACHINE_NAME = @LOGIN_MACHINE_NAME, LOGIN_MAC_ADDRESS = @LOGIN_MAC_ADDRESS, LOGIN_TIME = @LOGIN_TIME, LOGOUT_TIME = @LOGOUT_TIME, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE LOGIN_SEQUENCE = @LOGIN_SEQUENCE  ";
        private string UPDATED_BY = "@UPDATED_BY";

        public bool CheckSysLoginLogIDUnique(string sysLoginLogID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOGIN_SEQUENCE", sysLoginLogID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_SYS_LOGIN_LOG_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckSysLoginLogIDUnique(DataBase dataBase, DbTransaction tran, string sysLoginLogID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOGIN_SEQUENCE", sysLoginLogID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_SYS_LOGIN_LOG_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteSysLoginLog(List<string> sysLoginLogID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < sysLoginLogID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOGIN_SEQUENCE", sysLoginLogID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_SYS_LOGIN_LOG.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteSysLoginLog(DataBase dataBase, DbTransaction tran, List<string> sysLoginLogID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < sysLoginLogID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOGIN_SEQUENCE", sysLoginLogID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_SYS_LOGIN_LOG.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllSysLoginLog(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_LOGIN_LOG.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllSysLoginLog(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_LOGIN_LOG.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public SysLoginLogInfo GetSysLoginLogByID(string sysloginlogID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_LOGIN_LOG.Replace("@_@", whLoginID) + " WHERE LOGIN_SEQUENCE = @LOGIN_SEQUENCE  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOGIN_SEQUENCE", sysloginlogID) };
            SysLoginLogInfo sysLoginLogInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    sysLoginLogInfo = new SysLoginLogInfo((reader["LOGIN_SEQUENCE"].ToString() != "") ? int.Parse(reader["LOGIN_SEQUENCE"].ToString()) : 0, reader["LOGIN_USER_ID"].ToString(), reader["LOGIN_USER_NAME"].ToString(), reader["LOGIN_STATUS"].ToString(), reader["LOGIN_MODE"].ToString(), reader["LOGIN_IP_ADDRESS"].ToString(), reader["LOGIN_MACHINE_NAME"].ToString(), reader["LOGIN_MAC_ADDRESS"].ToString(), (reader["LOGIN_TIME"].ToString() != "") ? DateTime.Parse(reader["LOGIN_TIME"].ToString()) : (CS00003 = new DateTime()), (reader["LOGOUT_TIME"].ToString() != "") ? DateTime.Parse(reader["LOGOUT_TIME"].ToString()) : (CS00003 = new DateTime()), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return sysLoginLogInfo;
        }

        public SysLoginLogInfo GetSysLoginLogByID(DataBase dataBase, DbTransaction tran, string sysloginlogID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_SYS_LOGIN_LOG.Replace("@_@", whLoginID) + " WHERE LOGIN_SEQUENCE = @LOGIN_SEQUENCE  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOGIN_SEQUENCE", sysloginlogID) };
            SysLoginLogInfo sysLoginLogInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                sysLoginLogInfo = new SysLoginLogInfo((reader["LOGIN_SEQUENCE"].ToString() != "") ? int.Parse(reader["LOGIN_SEQUENCE"].ToString()) : 0, reader["LOGIN_USER_ID"].ToString(), reader["LOGIN_USER_NAME"].ToString(), reader["LOGIN_STATUS"].ToString(), reader["LOGIN_MODE"].ToString(), reader["LOGIN_IP_ADDRESS"].ToString(), reader["LOGIN_MACHINE_NAME"].ToString(), reader["LOGIN_MAC_ADDRESS"].ToString(), (reader["LOGIN_TIME"].ToString() != "") ? DateTime.Parse(reader["LOGIN_TIME"].ToString()) : (CS00003 = new DateTime()), (reader["LOGOUT_TIME"].ToString() != "") ? DateTime.Parse(reader["LOGOUT_TIME"].ToString()) : (CS00003 = new DateTime()), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return sysLoginLogInfo;
        }

        public DataSet GetSysLoginLogByQueryList(List<string> sqlWhere, SysLoginLogQueryEntity sysloginlogQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_SYS_LOGIN_LOG.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!sysloginlogQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, sysloginlogQuery.CurrentPage, sysloginlogQuery.PageSize, sysloginlogQuery.SortField, sysloginlogQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetSysLoginLogByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, SysLoginLogQueryEntity sysloginlogQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_SYS_LOGIN_LOG.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!sysloginlogQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, sysloginlogQuery.CurrentPage, sysloginlogQuery.PageSize, sysloginlogQuery.SortField, sysloginlogQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertSysLoginLog(SysLoginLogInfo sysLoginLogInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_SysLoginLog_Parameters(sysLoginLogInfo);
                if (paras != null)
                {
                    result = DBHelper.ExecuteNonQuery(CommandType.Text, this.SQL_INSERT_SYS_LOGIN_LOG.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertSysLoginLog(DataBase dataBase, DbTransaction tran, SysLoginLogInfo sysLoginLogInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_SysLoginLog_Parameters(sysLoginLogInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_SYS_LOGIN_LOG.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_SysLoginLog_Parameters(SysLoginLogInfo sysLoginLogInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.LOGIN_USER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LOGIN_USER_NAME, SqlDbType.VarChar, 40), new SqlParameter(this.LOGIN_STATUS, SqlDbType.VarChar, 10), new SqlParameter(this.LOGIN_MODE, SqlDbType.VarChar, 50), new SqlParameter(this.LOGIN_IP_ADDRESS, SqlDbType.VarChar, 50), new SqlParameter(this.LOGIN_MACHINE_NAME, SqlDbType.VarChar, 100), new SqlParameter(this.LOGIN_MAC_ADDRESS, SqlDbType.VarChar, 50), new SqlParameter(this.LOGIN_TIME, SqlDbType.DateTime, 8), new SqlParameter(this.LOGOUT_TIME, SqlDbType.DateTime, 8), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            paramArray[0].Value = sysLoginLogInfo.LoginUserID;
            if (!string.IsNullOrEmpty(sysLoginLogInfo.LoginUserName))
            {
                paramArray[1].Value = sysLoginLogInfo.LoginUserName;
            }
            else
            {
                paramArray[1].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(sysLoginLogInfo.LoginStatus))
            {
                paramArray[2].Value = sysLoginLogInfo.LoginStatus;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(sysLoginLogInfo.LoginMode))
            {
                paramArray[3].Value = sysLoginLogInfo.LoginMode;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(sysLoginLogInfo.LoginIpAddress))
            {
                paramArray[4].Value = sysLoginLogInfo.LoginIpAddress;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(sysLoginLogInfo.LoginMachineName))
            {
                paramArray[5].Value = sysLoginLogInfo.LoginMachineName;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(sysLoginLogInfo.LoginMacAddress))
            {
                paramArray[6].Value = sysLoginLogInfo.LoginMacAddress;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            DateTime CS00003 = new DateTime();
            if (sysLoginLogInfo.LoginTime != CS00003)
            {
                paramArray[7].Value = sysLoginLogInfo.LoginTime;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            if (sysLoginLogInfo.LogoutTime != new DateTime())
            {
                paramArray[8].Value = sysLoginLogInfo.LogoutTime;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            paramArray[9].Value = sysLoginLogInfo.CreatedBy;
            if (!string.IsNullOrEmpty(sysLoginLogInfo.UpdatedBy))
            {
                paramArray[10].Value = sysLoginLogInfo.UpdatedBy;
                return paramArray;
            }
            paramArray[10].Value = DBNull.Value;
            return paramArray;
        }

        public int UpdateSysLoginLog(SysLoginLogInfo sysLoginLogInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_SysLoginLog_Parameters(sysLoginLogInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_SYS_LOGIN_LOG.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateSysLoginLog(DataBase dataBase, DbTransaction tran, SysLoginLogInfo sysLoginLogInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_SysLoginLog_Parameters(sysLoginLogInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_SYS_LOGIN_LOG.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

